package com.donfelix.animationdemo.animation;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.transition.Fade;
import android.transition.TransitionManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.donfelix.animationdemo.R;
import com.donfelix.animationdemo.common.BaseDetailActivity;
import com.donfelix.animationdemo.common.Sample;
import com.donfelix.animationdemo.databinding.ActivityAnimations1Binding;

/**
 * Created by Don on 2017/3/23.
 * Desc
 */

public class AnimationsActivity1 extends BaseDetailActivity implements View.OnClickListener {
    private ActivityAnimations1Binding binding;
    private Sample sample;
    private ImageView square;
    private LinearLayout viewRoot;
    private boolean sizeChanged;
    private int savedWidth;
    private boolean positionChanged;

    @Override
    protected void bindData() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_animations1);
        sample = (Sample) getIntent().getExtras().getSerializable(EXTRA_SAMPLE);
        binding.setAnimationsSample(sample);
    }

    @Override
    protected void setupWindowAinimation() {
        getWindow().setReenterTransition(new Fade());
    }

    @Override
    protected void setupLayout() {
        square = binding.squareGreen;
        viewRoot = binding.sample3Root;
        binding.sample3Button1.setOnClickListener(this);
        binding.sample3Button2.setOnClickListener(this);
        binding.sample3Button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sample3_button1:
                TransitionManager.beginDelayedTransition(viewRoot);
                ViewGroup.LayoutParams params = square.getLayoutParams();
                if (sizeChanged) {
                    params.width = savedWidth;
                } else {
                    savedWidth = params.width;
                    params.width = 200;
                }
                sizeChanged = !sizeChanged;
                square.setLayoutParams(params);
                break;
            case R.id.sample3_button2:
                TransitionManager.beginDelayedTransition(viewRoot);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) square.getLayoutParams();
                if (positionChanged) {
                    layoutParams.gravity = Gravity.CENTER;
                } else {
                    layoutParams.gravity = Gravity.LEFT;
                }
                positionChanged = !positionChanged;
                square.setLayoutParams(layoutParams);
                break;
            case R.id.sample3_button3:
                Intent intent = new Intent(this, AnimationsActivity2.class);
                intent.putExtra(EXTRA_SAMPLE, sample);
                transitionTo(intent);
                break;
            default:
                break;
        }
    }
}
